/**
 * 双端链表
 */
package com.study.s05.chain;

public class FirstLastLinkList {
	
	//头结点
	private Node first;
	//尾结点
	private Node last;

	public FirstLastLinkList() {
		first = null;
		last = null;
	}
	/**
	 * 插入一个结点，在头结点之后
	 */
	public void insertFirst(long value) {
		Node node = new Node(value);
		if(isEmpty()) {
			last = node;
		}
		node.next = first;
		first = node;
	}
	/**
	 * 插入一个结点，在尾结点之后
	 */
	public void insertLast(long value) {
		Node node = new Node(value);
		if(isEmpty()) {
			first = node;
		}else {
			last.next = node;
		}
		last = node;
	}
	/**
	 * 删除一个结点，在头结点
	 */
	public Node deleteFirst() {
		Node tmpNode = first;
		if(first.next == null) {
			last = null;
		}
		first = tmpNode.next;
		return tmpNode;
	}
	/**
	 * 显示方法
	 */
	public void display() {
		Node current = first;
		while(current != null) {
			current.display();
			current = current.next;
		}
		System.out.println();
	}
	/**
	 * 查找
	 */
	public Node find(long value) {
		Node current = first;
		while(current.data != value) {
			if(current.next == null) {
				return null;
			}
			current = current.next;
		}
		return current;
	}
	/**
	 * 删除
	 */
	public Node delete(long value) {
		Node current = first;
		Node previous = first;
		while(current.data != value) {
			if(current.next == null) {
				return null;
			}
			previous = current;
			current = current.next;
		}
		if(current == first) {
			first = first.next;
		} else {
			previous.next = current.next;
		}
		return current;
	}
	/**
	 * 判断是否为空
	 */
	public boolean isEmpty() {
		return (first == null);
	}
	
}
